'use strict';
const db = uniCloud.database();

exports.main = async (event, context) => {
	console.log('update-content event:', event);
	
	// 参数验证
	if (!event.contentId) {
		return {
			code: 400,
			msg: '缺少内容ID',
			data: null
		}
	}
	
	if (!event.type) {
		return {
			code: 400,
			msg: '缺少内容类型',
			data: null
		}
	}

	// 根据内容类型选择数据库集合
	let collection;
	switch (event.type) {
		case 'post':
			collection = 'add-content';
			break;
		case 'activity':
			collection = 'add-content';
			break;
		case 'task':
			collection = 'taskList';
			break;
		default:
			return {
				code: 400,
				msg: '不支持的内容类型',
				data: null
			}
	}

	// 构建更新数据
	const updateData = {};
	
	// 根据内容类型设置可更新的字段
	if (event.type === 'post') {
		if (event.title !== undefined) updateData.title = event.title;
		if (event.content !== undefined) updateData.content = event.content;
		if (event.category !== undefined) updateData.category = event.category;
		
		// 基础验证
		if (!updateData.content || !updateData.content.trim()) {
			return {
				code: 400,
				msg: '帖子内容不能为空',
				data: null
			}
		}
		
		if (!updateData.category) {
			return {
				code: 400,
				msg: '帖子分类不能为空',
				data: null
			}
		}
	} else if (event.type === 'activity') {
		if (event.title !== undefined) updateData.title = event.title;
		if (event.content !== undefined) updateData.content = event.content;
		if (event.category !== undefined) updateData.category = event.category;
		if (event.activity_time !== undefined) updateData.activity_time = event.activity_time;
		if (event.location !== undefined) updateData.location = event.location;
		if (event.max_attendees !== undefined) updateData.max_attendees = event.max_attendees;
	} else if (event.type === 'task') {
		if (event.title !== undefined) updateData.title = event.title;
		if (event.description !== undefined) updateData.description = event.description;
		if (event.taskType !== undefined) updateData.type = event.taskType;
		if (event.reward !== undefined) updateData.reward = event.reward;
		if (event.deadline !== undefined) updateData.deadline = event.deadline;
		if (event.requirements !== undefined) updateData.requirements = event.requirements;
		
		// 任务基础验证
		if (!updateData.title || !updateData.title.trim()) {
			return {
				code: 400,
				msg: '任务标题不能为空',
				data: null
			}
		}
		
		if (!updateData.description || !updateData.description.trim()) {
			return {
				code: 400,
				msg: '任务描述不能为空',
				data: null
			}
		}
		
		if (!updateData.type) {
			return {
				code: 400,
				msg: '任务类型不能为空',
				data: null
			}
		}
		
		if (!updateData.reward || updateData.reward <= 0) {
			return {
				code: 400,
				msg: '任务报酬必须大于0',
				data: null
			}
		}
	}
	
	// 添加更新时间
	updateData.update_time = Date.now();

	try {
		// 先检查内容是否存在
		const existDoc = await db.collection(collection).doc(event.contentId).get();
		if (existDoc.data.length === 0) {
			return {
				code: 404,
				msg: '未找到对应内容',
				data: null
			}
		}

		// 执行更新
		const res = await db.collection(collection).doc(event.contentId).update(updateData);
		
		if (res.updated === 1) {
			// 获取更新后的数据
			const updatedDoc = await db.collection(collection).doc(event.contentId).get();
			
			return {
				code: 200,
				msg: '更新成功',
				data: updatedDoc.data[0]
			}
		} else {
			return {
				code: 500,
				msg: '更新失败，未影响任何记录',
				data: res
			}
		}
	} catch (err) {
		console.error('更新内容失败:', err);
		return {
			code: 500,
			msg: '更新失败: ' + err.message,
			data: err
		}
	}
};
